package com.yuanduan.mplus.view;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.cj.framework.util.SP;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.DBManager;
import com.yuanduan.mplus.model.entity.RSM;
import com.yuanduan.mplus.model.entity.User;

import java.util.HashMap;

public class ResetPwd1Activity extends BaseActivity implements View.OnClickListener {
    //忘记密码以后的重置密码界面
    private EditText et_newPwd;
    private EditText et_insurePwd;
    private Button btn_reset;
    private ImageButton ib_back;

    private String newPwd;
    private String insurePwd;

    private IHttp iHttp=new HttpImp();
    private IHttp.HttpInfoCallback callback=new IHttp.HttpInfoCallback() {
        @Override
        public void onSucceed(Object result) {
            RSM rsm= (RSM) Config.decry(result,RSM.class);
            if(rsm.getRS()==1){
                Toast.makeText(ResetPwd1Activity.this,"修改密码成功",Toast.LENGTH_SHORT).show();
                SP.delete("isRemember");
                SP.delete("rememberUserPhone");
                SP.delete("rememberUserPwd");
                openActivity(StartActivity.class);
            }else{
                int r=rsm.getRS();
                Toast.makeText(ResetPwd1Activity.this,"修改密码失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailed(Exception e) {

        }
    };

    @Override
    protected void findId() {
        et_newPwd= (EditText) findViewById(R.id.resetPwd1_et_newPwd);
        et_insurePwd= (EditText) findViewById(R.id.resetPwd1_et_insurePwd);
        btn_reset= (Button) findViewById(R.id.resetPwd1_btn_reset);
        ib_back= (ImageButton) findViewById(R.id.resetPwd1_ib_back);
    }

    @Override
    protected void logic() {
        btn_reset.setOnClickListener(this);
        ib_back.setOnClickListener(this);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_reset_pwd1;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.resetPwd1_btn_reset:
                newPwd=et_newPwd.getText().toString().trim();
                insurePwd=et_insurePwd.getText().toString().trim();
                if((!TextUtils.isEmpty(newPwd))&&(!TextUtils.isEmpty(insurePwd))&&newPwd.equals(insurePwd)){
                    HashMap<String,String> hashMap=new HashMap<>();
                    hashMap.put("UserID",getIntent().getExtras().getString("UserID"));
                    hashMap.put("Password1",newPwd);
                    hashMap.put("Password2",insurePwd);
                    iHttp.httpInfoPost(Config.URL+Config.RESET_PWD,Config.translateMap(hashMap),callback);
                }
                break;
            case R.id.resetPwd1_ib_back:
                finish();
                break;
        }
    }
}
